package towerDefense.maps.tiles;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import util.qualitycontainers.MorphableObject;
import util.qualitycontainers.qualities.generics.BasicQuality;
import util.qualitycontainers.qualities.generics.intIDQ;
import util.qualitycontainers.qualities.generics.BufferedImageQ;
import util.qualitycontainers.qualities.generics.NameQ;


/**
 * Wraps a terrain object, tieing together ID#, name, and image along with other
 * tile properties
 * 
 * @author Julian
 */
public class TerrainTile extends MorphableObject
{
    public TerrainTile (Integer id, String type, String file)
        throws IOException
    {
        this(id, type, new File(file));
    }


    public TerrainTile (Integer id, String type, File file) throws IOException
    {
        super(new intIDQ(id), new NameQ(type), new BufferedImageQ(file));
        unoccupy();
    }


    public void setName (String type)
    {
        this.setQualities(new NameQ(type));
    }


    public String getName ()
    {
        return (String) ((BasicQuality) this.getQuality(NameQ.class)).get();
    }


    public void setImage (File read)
    {
        try
        {
            this.setQualities(new BufferedImageQ(read));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }


    /**
     * Set image by String filepath
     * 
     * @param file
     */
    public void setImage (String file)
    {
        setImage(new File(file));
    }


    /**
     * returns the comparison between the names of the terrain
     */

    @Override
    public int compareTo (MorphableObject o)
    {
        canCompare(o);
        return this.getQuality(NameQ.class)
                   .compareTo(o.getQuality(NameQ.class));
    }


    public Integer getID ()
    {
        return ((intIDQ) this.getQuality(intIDQ.class)).id();
    }


    public boolean isOccupied ()
    {
        return amActive;
    }


    public void occupy ()
    {
        amActive = true;
    }


    public void unoccupy ()
    {
        amActive = false;
    }


    public Image getImage ()
    {
        return ((BufferedImageQ) getQuality(BufferedImageQ.class)).image();
    }


    public String getImageFile ()
    {
        return ((File) ((BufferedImageQ) getQuality(BufferedImageQ.class)).get()).getPath();
    }


    @Override
    public String toString ()
    {
        return this.getName() + "|" + this.getID() + "|" + this.getImageFile();
    }
}
